package movie.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.Cinema;
import movie.entity.Film;
import movie.entity.Hall;
import movie.service.CinemaInsertService;
import movie.service.CinemaNameService;
import movie.service.CinemaScheduleFindAllService;
import movie.service.FilmFindAllService;
import movie.service.HallService;

@Controller
public class CinemaInsertController {
	
	@Autowired
	private CinemaInsertService cinemaInsertService;
	@Autowired
	private CinemaScheduleFindAllService cinemaScheduleFindAllService;
	@Autowired
	private FilmFindAllService filmFindAllService;
	@Autowired
	private CinemaNameService cinemaNameService;
	@Autowired
	private HallService hallService;
	
	
	@RequestMapping(value = "/cinemainsert", method = RequestMethod.GET)
	public String get(Model model,HttpServletRequest request) 
	{
		HttpSession session = request.getSession();

		if(session.getAttribute("cinemaId") == null) {
			return "forward:clogin.jsp";
		} 
		int movieId = (int) session.getAttribute("movieId");
		
		int cinemaId=(int) session.getAttribute("cinemaId");    
		
		Cinema cinema = new Cinema();
		cinema.setMovieId(movieId);
		cinema.setCinemaId(cinemaId);
		List<Cinema> cinemas=cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema);
		for(Cinema cinema11:cinemas)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
			cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
		}
		model.addAttribute("cinemas", cinemas);
		 
		
		String filmName=filmFindAllService.findMovieId(movieId).getFilmName();
		model.addAttribute("filmName", filmName);
			
		List<String> cinemaNames=hallService.findByCinemaIdToHallName(cinema.getCinemaId());
		model.addAttribute("cinemaNames", cinemaNames);
		model.addAttribute("cinemaName",cinemaNameService.findByCinemaIdToCinemaName(cinemaId));
		
		
		return "Cinema/cinemainsert";
	}
	
	@RequestMapping(value = "/cinemainsert", method = RequestMethod.POST)
	public String post(Cinema cinema,Model model,HttpServletRequest request,String cinemaName1,String hallName1) {
		

		if(cinema.getFilmBeginTime()==null || cinema.getBeginHour()==null || cinema.getBeginMin()==null || cinema.getMovieId()==null
				|| cinemaName1.equals("") || hallName1.equals("") || cinema.getFilmEndTime()==null || cinema.getFilmLanguage().equals("") 
				|| cinema.getPrice()==null || cinema.getEndHour()==null || cinema.getEndMin()==null ||cinema.getFilmVersion().equals("") )
		{
			HttpSession session = request.getSession(false);

			if(session.getAttribute("cinemaId") == null) {
				return "forward:clogin.jsp";
			} 
			int movieId = (int) session.getAttribute("movieId");
			
			int cinemaId=(int) session.getAttribute("cinemaId");    
			
			Cinema cinema22 = new Cinema();
			cinema22.setMovieId(movieId);
			cinema22.setCinemaId(cinemaId);
			List<Cinema> cinemas=cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema22);
			for(Cinema cinema11:cinemas)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
				cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
			}
			model.addAttribute("cinemas", cinemas);
			 
			
			String filmName=filmFindAllService.findMovieId(movieId).getFilmName();
			model.addAttribute("filmName", filmName);
				
			List<String> cinemaNames=hallService.findByCinemaIdToHallName(cinema22.getCinemaId());
			model.addAttribute("cinemaNames", cinemaNames);
			model.addAttribute("cinemaName",cinemaNameService.findByCinemaIdToCinemaName(cinemaId));
		
			model.addAttribute("cuowu", 1);
			return "Cinema/cinemainsert";
		}
			
			
			
		Hall hall=new Hall();
		int cinemaId=cinemaNameService.findByCinemaNameToCinemaId(cinemaName1);
		cinema.setCinemaId(cinemaId);
		hall.setCinemaId(cinemaId);
		hall.setHallName(hallName1);
		int hallId=hallService.findByCinemaIdHallNameToHallId(hall);
		cinema.setHallId(hallId);
		int rows=cinemaInsertService.insertcinema(cinema);
		HttpSession session = request.getSession();
		
		List<Cinema> cinemas=cinemaScheduleFindAllService.find((int) session.getAttribute("movieId"));
		
		
		model.addAttribute("cinemas", cinemas);
		
		return "redirect:/cinemaFindAllFilm";
	}
}